package com.community.web.master.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.model.entity.Goods;
import com.community.model.entity.Special;
import com.community.model.entity.SpecialGoods;
import com.community.web.master.domain.dto.PageGoodsDto;
import com.community.web.master.domain.dto.PageSpecialDto;
import com.community.web.master.domain.dto.SpecialDto;
import com.community.web.master.domain.dto.SpecialGoodsDto;
import com.community.web.master.domain.vo.GoodsWithCategoryNameVo;
import com.community.web.master.domain.vo.PageResult;
import com.community.web.master.domain.vo.SpecialGoodsVo;
import com.community.web.master.exception.DataException;
import com.community.web.master.mapper.GoodsMapper;
import com.community.web.master.mapper.SpecialGoodsMapper;
import com.community.web.master.mapper.SpecialMapper;
import com.community.web.master.service.ISpecialService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author 李晶
 * @since 2024-07-02
 */
@Service
public class SpecialServiceImpl extends ServiceImpl<SpecialMapper, Special> implements ISpecialService {
    @Autowired
    private SpecialMapper specialMapper;
    @Autowired
    private SpecialGoodsMapper specialGoodsMapper;
    @Autowired
    private GoodsMapper goodsMapper;

    /**
     * 分页查询
     * @param specialDto
     * @return
     */
    @Override
    public PageResult pageQuery(PageSpecialDto specialDto) {
        IPage<Special> page = new Page<Special>(specialDto.getPage(),specialDto.getPageSize());
        QueryWrapper<Special> queryWrapper = new QueryWrapper<Special>();
        if(specialDto.getSpecialName()!=null && !specialDto.getSpecialName().equals("")){
            queryWrapper.like("special_name",specialDto.getSpecialName());
        }
        if(specialDto.getSpecialStatus()!=null){
            queryWrapper.eq("special_status",specialDto.getSpecialStatus());
        }
        specialMapper.selectPage(page,queryWrapper);

        return new PageResult(page.getTotal(),page.getRecords());
    }

    /**
     * 增加促销模块
     * @param specialDto
     */
    @Override
    public void addSpecial(SpecialDto specialDto) {
       if(specialMapper.selectOne(new QueryWrapper<Special>().eq("special_name",specialDto.getSpecialName())) !=null)
           throw new DataException("模块已存在");
       Special special = new Special();
       BeanUtils.copyProperties(specialDto,special);
       special.setId(null);
       specialMapper.insert(special);
    }

    /**
     * 修改模块
     * @param specialDto
     */
    @Override
    public void updateSpecial(SpecialDto specialDto) {
        if(specialDto.getId()==null) throw new DataException("数据有误");
        Special oldSpecial = specialMapper.selectOne(new QueryWrapper<Special>().eq("id", specialDto.getId()));
        if(oldSpecial ==null)
            throw new DataException("模块不存在");
        //名称修改了,要查查能不能改
        if(!specialDto.getSpecialName().equals(oldSpecial.getSpecialName())){
            if(specialMapper.selectOne(new QueryWrapper<Special>().eq("special_name",specialDto.getSpecialName())) !=null)
                throw new DataException("模块已存在");
        }
        Special special = new Special();
        BeanUtils.copyProperties(specialDto,special);
        specialMapper.updateById(special);
    }

    /**
     * 分页查询促销模块下的商品
     * @param id
     * @param pageGoodsDto
     * @return
     */
    @Override
    public PageResult pageQueryGoodsById(Integer id, PageGoodsDto pageGoodsDto) {
        //查询出商品号
        List<SpecialGoods> specialGoods = specialGoodsMapper.selectList(new QueryWrapper<SpecialGoods>().eq("special_id", id));
        List<String> goodsNos = specialGoods.stream().map(SpecialGoods::getGoodsNo).collect(Collectors.toList());
        //查出total
        QueryWrapper<Goods> queryWrapper= new QueryWrapper<Goods>();
        if(pageGoodsDto.getKeyValue() !=null){
            if(!pageGoodsDto.getKeyValue().equals("")){
                queryWrapper.like("goods_name",pageGoodsDto.getKeyValue());
            }
        }
        if(pageGoodsDto.getCategoryId()!=null){
            if(pageGoodsDto.getCategoryId()==0) throw new DataException("信息有误");
            queryWrapper.eq("category_id",pageGoodsDto.getCategoryId());
        }
        queryWrapper.in("goods_no",goodsNos);
        Long total = goodsMapper.selectCount(queryWrapper);

        //查list
        Map<String,Object> map = new HashMap<>();
        map.put("goods_name",pageGoodsDto.getKeyValue());
        map.put("category_id",pageGoodsDto.getCategoryId());
        map.put("goods_nos",goodsNos);
        map.put("limit",(pageGoodsDto.getPageSize()));
        map.put("offset",(pageGoodsDto.getPage()-1)*pageGoodsDto.getPageSize());

        List<GoodsWithCategoryNameVo> goods = goodsMapper.selectPageByMap(map);
        //分页查询的列表结果
        List<SpecialGoodsVo> specialGoodsVos = goods.stream()
                .map(goods1 -> {
                    SpecialGoodsVo temp = new SpecialGoodsVo();
                    temp.setId(goods1.getId());
                    temp.setGoodsNo(goods1.getGoodsNo());
                    temp.setGoodsName(goods1.getGoodsName());
                    temp.setCategoryName(goods1.getCategoryName());
                    temp.setGoodsIntroduce(goods1.getGoodsIntroduce());
                    temp.setGoodsPicture(goods1.getGoodsPicture());
                    return temp;
                })
                .collect(Collectors.toList());
        return new PageResult(total,specialGoodsVos);
    }

    /**
     * 批量增加促销模块中的商品
     * @param id
     * @param goodsNos
     */
    @Override
    @Transactional
    public void addSpecialGoods(Integer id, List<String> goodsNos) {
        Special oldSpecial = specialMapper.selectOne(new QueryWrapper<Special>().eq("id", id));
        if(oldSpecial ==null)
            throw new DataException("模块不存在");
        for (String goodsNo : goodsNos) {
            System.out.println(goodsNos);
            SpecialGoods specialGoods = new SpecialGoods();
            specialGoods.setSpecialId(id);
            specialGoods.setGoodsNo(goodsNo);
            specialGoodsMapper.insert(specialGoods);
        }
    }

    /**
     * 删除模块下的商品
     * @param specialGoodsDto
     */
    @Override
    public void deleteSpecialGoods(SpecialGoodsDto specialGoodsDto) {
        QueryWrapper<SpecialGoods> queryWrapper =new QueryWrapper<SpecialGoods>();
        queryWrapper.eq("special_id",specialGoodsDto.getId());
        queryWrapper.eq("goods_no",specialGoodsDto.getGoodsNo());
        specialGoodsMapper.delete(queryWrapper);
    }

    /**
     * 删除促销模块
     * @param id
     */
    @Override
    @Transactional
    public void deleteSpecial(Integer id) {
        specialGoodsMapper.delete(new QueryWrapper<SpecialGoods>().eq("special_id",id));
        specialMapper.deleteById(id);
    }
}
